package com.cw.jeeyt.service.impl.process;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.process.ActReProcdefBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.process.ActReProcdefDao;
import com.cw.jeeyt.dao.model.process.ActReProcdefDo;
import com.cw.jeeyt.service.converter.process.ActReProcdefConverter;
import com.cw.jeeyt.service.intf.process.ActReProcdefService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 流程定义业务实现类
 *
 * @author chenw
 * @date 2019-03-13 22:15:39
 */
@Service
public class ActReProcdefServiceImpl implements ActReProcdefService {

    private static final Logger logger = LoggerFactory.getLogger(ActReProcdefServiceImpl.class);
    @Autowired
    private ActReProcdefDao actReProcdefDao;//流程定义数据库操作接口

    /**
     * 查询所有流程定义
     */
    @Override
    public List<ActReProcdefBo> queryActReProcdefList() throws Exception {
        return ActReProcdefConverter.doList2BoList(actReProcdefDao.queryActReProcdefList());
    }

    /**
     * 分页查询流程定义
     *
     * @param actReProcdefBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ActReProcdefBo> queryByPage(ActReProcdefBo actReProcdefBo, Integer startNum, Integer pageSize,
        Order order) throws Exception {
        if (actReProcdefBo == null) {
            logger.error("流程定义为空");
            return null;
        }
        PageInfo<ActReProcdefDo> pageInfo = actReProcdefDao.queryByPage(ActReProcdefConverter.bo2Do(actReProcdefBo),
            startNum, pageSize, order);
        return new PageInfo<>(pageInfo, ActReProcdefConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询流程定义信息
     *
     * @return
     */
    @Override
    public ActReProcdefBo getActReProcdefById(String id_) throws Exception {

        if (StringUtils.isEmpty(id_)) {
            logger.error("--主键id_为空--");
            return null;
        }
        return ActReProcdefConverter.do2Bo(actReProcdefDao.getById(id_));
    }

    /**
     * 新增流程定义
     *
     * @param actReProcdefBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ActReProcdefBo actReProcdefBo) throws Exception {

        if (actReProcdefBo == null) {
            logger.error("流程定义为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReProcdefDo actReProcdef = ActReProcdefConverter.bo2Do(actReProcdefBo);
        actReProcdef.setId_(SequenceTool.nextId());
        Integer result = actReProcdefDao.insert(actReProcdef);
        return result;
    }

    /**
     * 新增流程定义不为空的字段
     *
     * @param actReProcdefBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ActReProcdefBo actReProcdefBo) throws Exception {

        if (actReProcdefBo == null) {
            logger.error("流程定义为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReProcdefDo actReProcdef = ActReProcdefConverter.bo2Do(actReProcdefBo);
        actReProcdef.setId_(SequenceTool.nextId());
        Integer result = actReProcdefDao.insertSelective(actReProcdef);
        return result;
    }

    /**
     * 更新流程定义
     *
     * @param actReProcdefBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ActReProcdefBo actReProcdefBo) throws Exception {

        if (actReProcdefBo == null) {
            logger.error("流程定义为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReProcdefDo actReProcdef = ActReProcdefConverter.bo2Do(actReProcdefBo);
        actReProcdefDao.update(actReProcdef);
    }

    /**
     * 更新流程定义不为空的字段
     *
     * @param actReProcdefBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ActReProcdefBo actReProcdefBo) throws Exception {

        if (actReProcdefBo == null) {
            logger.error("流程定义为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActReProcdefDo actReProcdef = ActReProcdefConverter.bo2Do(actReProcdefBo);
        actReProcdefDao.updateSelective(actReProcdef);
    }

    /**
     * 删除流程定义
     *
     * @param actReProcdefBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ActReProcdefBo actReProcdefBo) throws Exception {

        if (actReProcdefBo == null) {
            logger.error("流程定义为空");
            return;
        }
        ActReProcdefDo actReProcdef = ActReProcdefConverter.bo2Do(actReProcdefBo);
        actReProcdefDao.delete(actReProcdef);
    }
}
